<template>
	<view>
		<cu-custom bgColor="text-156" :isBack="true">
			<block slot="content">修改密码</block>
		</cu-custom>
		<view>
			<uni-forms class="right-form" :value="formData" ref="form" :rules="rules" errShowType="toast">
				<view class="form-content">
					<uni-forms-item required label="原密码" name="oldPass">
						<uni-easyinput class="form-ipt" type="password" v-model="formData.oldPass" placeholder="请输入原密码" maxlength="20"/>
					</uni-forms-item>
					<uni-forms-item required label="新密码" name="newPass" >
						<uni-easyinput class="form-ipt" type="password" v-model="formData.newPass" placeholder="请输入新密码" maxlength="20"/>
					</uni-forms-item>
					<uni-forms-item required label="确认密码" name="checkPass">
						<uni-easyinput class="form-ipt" type="password" v-model="formData.checkPass" placeholder="请确认密码" maxlength="20"/>
					</uni-forms-item>
				</view>
				<button class="form-btn" @click="submitForm">确认</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	const passwordUrl = '/system/user/updatePass'
	export default {
		components:{
		},
		data() {
			return {
				rules: {
					oldPass: {
						rules: [
							{ required: true, errorMessage: '请输入原密码'},
						]
					},
					newPass: {
						rules: [
							{ required: true, errorMessage: '请输入新密码'},
						]
					},
					checkPass: {
						rules: [
							{ required: true, errorMessage: '请确认密码'},
						]
					},
				},
				formData:{
					oldPass: '',
					newPass: '',
					checkPass: ''
				},
			}
		},
		methods: {
			submitForm(){
				this.$refs.form.submit().then((res)=>{
					console.log('表单返回得值：', res)
					const {oldPass,newPass,checkPass} = res
					if(newPass !== checkPass){
						this.$api.toastMsg('两次密码输入不一致')
						return
					}
					if(oldPass === newPass){
						this.$api.toastMsg('密码没有变化')
						return
					}
					uni.showLoading({
						title:'保存中',
						mask: true
					})
					this.$api.request(passwordUrl,{
						method: 'post',
						data: this.formData
					}).then(res=>{
						uni.hideLoading()
						if(res.success){
							uni.clearStorageSync('USER_TOKEN')
							uni.clearStorageSync('USER_ROLE')
							uni.reLaunch({
								url:'/pages/login/login?exit=3'
							})
						}
					}).catch(()=>{
						uni.hideLoading()
					})
				}).catch(err=>{
					console.log(err)
				})
			}
		}
	}
</script>

<style lang="scss">
	
</style>
